#include "train.h"
#include "ui_train.h"
#include <QDebug>

Train::Train(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Train)
{
    ui->setupUi(this);

    m_nLeftSideSpaceX = 30;
    m_nRightSideSpaceX = 100;
    m_nSideSpaceY = 40;
    m_nCabinWidth = 50;
    m_nSaloonDorNumHeight = 20;
    m_nLabelSpaceX = 9;
    m_nEmrDorWidth = 20;
    m_nEmrDorHeight = 40;

    m_nCabStartx = m_nLeftSideSpaceX;
    m_nCabStarty = m_nSideSpaceY;

    m_nSalStartx = m_nCabStartx+m_nCabinWidth;
    m_nSalStarty = m_nSideSpaceY;
    m_nSaloonWidth = (this->width()-m_nLeftSideSpaceX-m_nRightSideSpaceX-2*m_nCabinWidth)/SaloonCnt;
    m_nSaloonHeight = this->height()- 2*m_nSideSpaceY;

    m_nLabelWidth = (m_nSaloonWidth - (SaloonDorCnt/2+1)*m_nLabelSpaceX)/(SaloonDorCnt/2);
    m_nLabelHeight = m_nLabelWidth;

    m_nSaloonDorWidth = m_nLabelWidth;
    m_nSaloonDorHeight = m_nLabelHeight;

    m_nSaloonDorNumWidth = m_nLabelWidth;

    m_nLabelSpaceY = (m_nSaloonHeight - 2*m_nSaloonDorNumHeight - LAYS*m_nLabelHeight)/(LAYS-1);

    InitSaloons();
    InitCabins();
    //InitEmrDors();
    InitDorNum();
    InitSaloonDors();
    InitServiceBrk();
    InitParkingBrk();
    InitAuxConverts();
    InitBatChargers();
    InitTracConverts();
    InitAirCompressers();
    InitTrainId();
    InitPans();
    InitHSCBS();
    InitTSKs();
    InitIES();
    InitPECUs();
    InitFires();
    InitSlippings();
    InitIdlings();
    InitDirection();
    InitACMode();
    setDirection(0);
//    setLeftDorOpenVisible(false);
//    setRightDorOpenVisible(false);
    //暂时不显示开门侧
    ui->UpDorOpen_Left->setVisible(false);
    ui->UpDorOpen_Right->setVisible(false);
    ui->BottomDorOpen_Left->setVisible(false);
    ui->BottomDorOpen_Right->setVisible(false);

    tbfbar_ = new TBFWidget(this);
    tbfbar_->setGeometry(this->width()-90,0,120,400);
    tbfbar_->show();
#ifdef RUN_IN_BOARD
    setDefaultStat();
#else
    setDisplayNormalStat();
#endif
}

Train::~Train()
{
    delete ui;
}

void Train::setDefaultStat()
{
    setTrainID(1);
    setCabStat(0,0);
    setCabStat(1,0);
    setDirection(0);
    for (int i=0;i<SaloonCnt ;++i ) {
        setHSCBStat(i,0);
        setIdlingStat(i,0);
        setSlipingStat(i,0);
        setParkingBrakeStat(i,0);
        setPECUStatus(i,1,0);
        setFireStat(i,0);
        for(int j=0;j<2;++j)
            setServiceBrkStat(i,j,0);
        for(int k=0;k<8;++k)
            setSaloonDorStat(i,k,0);
    }
    for (int i=0;i<4 ;++i ) {
        setTracConvertStat(i,0);
        setAuxConvertStat(i,0);
        setBatChargerStat(i,0);
    }
    for (int i=0;i<2 ;++i ) {
        setTSKStat(i,0);
        setAirCompressorStat(i,0);
        setPanStat(i,0);
        setIESStat(i,0);
    }
    setEffDmd(false,false,false,0);
}

void Train::setDisplayNormalStat()
{
    setTrainID(1);
    setCabStat(0,1);
    setDirection(0);
    for (int i=0;i<SaloonCnt ;++i ) {
        setHSCBStat(i,4);
        setIdlingStat(i,0);
        setSlipingStat(i,0);
        setParkingBrakeStat(i,3);
        setPECUStatus(i,1,0);
        setFireStat(i,0);
        for(int j=0;j<2;++j)
            setServiceBrkStat(i,j,3);
        for(int k=0;k<8;++k)
            setSaloonDorStat(i,k,8);
    }
    for (int i=0;i<4 ;++i ) {
        setTracConvertStat(i,2);
        setAuxConvertStat(i,2);
        setBatChargerStat(i,2);
    }
    for (int i=0;i<2 ;++i ) {
        setTSKStat(i,1);
        setAirCompressorStat(i,3);
        setPanStat(i,4);
        setIESStat(i,2);
    }
}

void Train::setDisplayUnNormalStat()
{
    setTrainID(1);
    setCabStat(0,1);
    setDirection(0);
    for (int i=0;i<SaloonCnt ;++i ) {
        setHSCBStat(i,4);
        setIdlingStat(i,1);
        setSlipingStat(i,1);
        setParkingBrakeStat(i,3);
        setPECUStatus(i,1,1);
        setFireStat(i,1);
        for(int j=0;j<2;++j)
            setServiceBrkStat(i,j,3);
        for(int k=0;k<8;++k)
            setSaloonDorStat(i,k,8);
    }
    for (int i=0;i<4 ;++i ) {
        setTracConvertStat(i,2);
        setAuxConvertStat(i,2);
        setBatChargerStat(i,2);
    }
    for (int i=0;i<2 ;++i ) {
        setTSKStat(i,1);
        setAirCompressorStat(i,3);
        setPanStat(i,4);
        setIESStat(i,2);
    }
}

//row 和 col 都从0开始
void Train::SetPosition(QWidget* w, int car, uint8_t row, uint8_t col, uint8_t total, bool xReverse)
{
    float rowSpaceX = (m_nSaloonWidth - total*m_nLabelWidth)/(total+1);
    if(!xReverse)
        w->setGeometry(m_nSalStartx+rowSpaceX+car*m_nSaloonWidth+col*(m_nLabelWidth+rowSpaceX),
                       m_nSalStarty+m_nSaloonDorNumHeight+row*(m_nLabelHeight+m_nLabelSpaceY),
                       m_nLabelWidth,m_nLabelHeight);
    else
        w->setGeometry(m_nSalStartx+rowSpaceX+car*m_nSaloonWidth+(total-1-col)*(m_nLabelWidth+rowSpaceX),
                       m_nSalStarty+m_nSaloonDorNumHeight+row*(m_nLabelHeight+m_nLabelSpaceY),
                       m_nLabelWidth,m_nLabelHeight);
};

void Train::InitSaloons()
{
    m_lSaloonList.clear();
    for(int i=0;i<SaloonCnt;i++)
    {
        QLabel *sal = new QLabel(this);
        sal->setStyleSheet("border:1px solid rgb(117,117,117);");
        sal->setGeometry(m_nSalStartx+m_nSaloonWidth*i,m_nSalStarty,m_nSaloonWidth,m_nSaloonHeight);
        sal->show();
        m_lSaloonList.append(sal);
    }
}

void Train::InitCabins()
{
    m_lCabinList.clear();
    for(int i=0;i<2;i++)
    {
        QLabel *cab = new QLabel(this);
        cab->setStyleSheet("border:1px solid rgb(117,117,117);color:white;font:12pt 'Arial';");
        cab->setAlignment(Qt::AlignCenter);
        cab->setText(QString("\n\n\n\n\n\n\n\n\n\nTC%1").arg(i+1));
        cab->setGeometry(m_nCabStartx+(SaloonCnt*m_nSaloonWidth+m_nCabinWidth)*i,m_nCabStarty,m_nCabinWidth,m_nSaloonHeight);
        m_lCabinList.append(cab);
    }
}

void Train::InitDirection()
{
    m_lDirectionList.clear();
    for(int i=0;i<2;i++)
    {
        QLabel *dir = new QLabel(this);
        if(i==0)
            dir->setStyleSheet("border-image: url(:/Images/Forward.png);");
        else
            dir->setStyleSheet("border-image: url(:/Images/Backward.png);");
        dir->setGeometry(m_nCabStartx+m_nCabinWidth/6+(SaloonCnt*m_nSaloonWidth+m_nCabinWidth)*i,
                         m_nCabStarty+m_nSaloonHeight*3/7,m_nCabinWidth*2/3,m_nSaloonHeight/7);
        m_lDirectionList.append(dir);
    }
}

void Train::InitACMode()
{
    m_lACModeList.clear();
     for(int i=0;i<2;i++)
     {
         QLabel *acmode = new QLabel(this);
         acmode->setStyleSheet("border-image: url(:/Images/WOS_off.png);");
         SetPosition(acmode,CAR(i%2?4:1),ROW(LAYS-2),COL(2),TOTAL(4),i>0);
         m_lACModeList.append(acmode);
     }
}

void Train::InitEmrDors()
{
  m_lEmrDorList.clear();
   for(int i=0;i<2;i++)
   {
       QLabel *emrdor = new QLabel(this);
       emrdor->setStyleSheet("background-color:rgb(157,157,157);border:1px solid rgb(157,157,157);");
       emrdor->setGeometry(m_nCabStartx+(SaloonCnt*m_nSaloonWidth+2*m_nCabinWidth-m_nEmrDorWidth-6)*i+3,
                           m_nCabStarty+m_nSaloonHeight/5+(1-i)*(m_nSaloonHeight*3/5-m_nEmrDorHeight),
                           m_nEmrDorWidth,m_nEmrDorHeight);
       m_lEmrDorList.append(emrdor);
   }
}

void Train::InitDorNum()
{
    for(int j =0;j<6*SaloonDorCnt/2;j++)
    {
        QLabel *dornum = new QLabel(this);
        dornum->setStyleSheet("background-color:transparent;font:10pt 'Arial';color:white;");
        dornum->setAlignment(Qt::AlignCenter);
        dornum->setGeometry(m_nSalStartx+m_nLabelSpaceX+(j/4)*m_nSaloonWidth+(j%4)*(m_nSaloonDorWidth+m_nLabelSpaceX),
                                m_nCabStarty+(m_nSaloonHeight-m_nSaloonDorNumHeight),
                                m_nSaloonDorNumWidth,m_nSaloonDorNumHeight);
        dornum->setText(QString("%1A").arg(j+1));
    }

    for(int j =0;j<6*SaloonDorCnt/2;j++)
    {
        QLabel *dornum = new QLabel(this);
        dornum->setStyleSheet("background-color:transparent;font:10pt 'Arial';color:white;");
        dornum->setAlignment(Qt::AlignCenter);
        dornum->setGeometry(m_nSalStartx+m_nLabelSpaceX+(j/4)*m_nSaloonWidth+(j%4)*(m_nSaloonDorWidth+m_nLabelSpaceX),
                                m_nCabStarty,
                                m_nSaloonDorNumWidth,m_nSaloonDorNumHeight);
        dornum->setText(QString("%1B").arg(24-j));
    }
}

void Train:: InitSaloonDors()
{
    uint8_t halfcar = SaloonCnt/2;
    for(int i=0;i<SaloonCnt;i++)
    {
        m_lSaloonDorList[i].clear();
        for(int j =0;j<SaloonDorCnt;j++)
        {
            QLabel *dor = new QLabel(this);
            dor->setStyleSheet("border-image: url(:/Images/Unknow.png);");
            if(i<halfcar)
                dor->setGeometry(m_nSalStartx+m_nLabelSpaceX+i*m_nSaloonWidth+(j/2)*(m_nSaloonDorWidth+m_nLabelSpaceX),
                                    m_nCabStarty+m_nSaloonDorNumHeight+(1-j%2)*(m_nSaloonHeight-2*m_nSaloonDorNumHeight-m_nSaloonDorHeight),
                                    m_nSaloonDorWidth,m_nSaloonDorHeight);
            else
                dor->setGeometry(m_nSalStartx+m_nLabelSpaceX+i*m_nSaloonWidth+((SaloonDorCnt/2-1)-j/2)*(m_nSaloonDorWidth+m_nLabelSpaceX),
                                    m_nCabStarty+m_nSaloonDorNumHeight+(j%2)*(m_nSaloonHeight-2*m_nSaloonDorNumHeight-m_nSaloonDorHeight),
                                    m_nSaloonDorWidth,m_nSaloonDorHeight);
            m_lSaloonDorList[i].append(dor);
        }
    }
}

void Train::InitServiceBrk()
{
    for(int i=0;i<SaloonCnt;i++)
    {
        m_lServiceBrake[i].clear();
        for(int j =0;j<2;j++)
        {
            QLabel *servicebrk = new QLabel(this);
            servicebrk->setStyleSheet("border-image: url(:/Images/Unknow.png);");
            SetPosition(servicebrk,CAR(i),ROW(1),COL(j==0?0:3),TOTAL(4),i>2);
            m_lServiceBrake[i].append(servicebrk);
        }
    }
}

void Train::InitParkingBrk()
{
    m_lParkingBrakeList.clear();
    for(int i=0;i<SaloonCnt;i++)
    {
        QLabel *parkbrk = new QLabel(this);
        parkbrk->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        SetPosition(parkbrk,CAR(i),ROW(2),COL(0),TOTAL(1));
        m_lParkingBrakeList.append(parkbrk);
    }
}

void Train::InitAuxConverts()
{
    for(int i=0;i<AuxCnt;i++)
    {
        QLabel *auxcvt = new QLabel(this);
        auxcvt->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        SetPosition(auxcvt,CAR((i%2)*2+(i/2)*3),ROW(LAYS-2),COL(1),TOTAL(4),i>1);
        m_lAuxConvertList.append(auxcvt);
    }
}

void Train::InitBatChargers()
{
    for(int i=0;i<BatChargerCnt;i++)
    {
        QLabel *charger = new QLabel(this);
        charger->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        SetPosition(charger,CAR((i%2)*2+(i/2)*3),ROW(LAYS-2),COL(2),TOTAL(4),i>1);
        m_lBatChargerList.append(charger);
    }
}

void Train::InitHSCBS()
{
    m_lHSCBList.clear();
    for(int i=0;i<4;i++)
    {
        QLabel *hscb = new QLabel(this);
        hscb->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        SetPosition(hscb,CAR(i+1),ROW(LAYS-3),COL(2),TOTAL(4));
        m_lHSCBList.append(hscb);
    }
}

void Train::InitTSKs()
{
    m_lTSKList.clear();
    for(int i=0;i<2;i++)
    {
        QLabel *tsk = new QLabel(this);
        tsk->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        SetPosition(tsk,CAR(i==0?0:5),ROW(LAYS-3),COL(0),TOTAL(1),i>0);
        m_lTSKList.append(tsk);
    }
}

void Train::InitTracConverts()
{
    for(int i=0;i<4;i++)
    {
        QLabel *traccvt = new QLabel(this);
        traccvt->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        SetPosition(traccvt,CAR(i+1),ROW(LAYS-3),COL(1),TOTAL(4));
        m_lTracConvertList.append(traccvt);
    }
}

void Train::InitAirCompressers()
{
    for(int i=0;i<2;i++)
    {
        QLabel *aircmp = new QLabel(this);
        aircmp->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        SetPosition(aircmp,CAR(i==0?0:5),ROW(3),COL(0),TOTAL(1));
        m_lAirCompressList.append(aircmp);
    }
}

void Train::InitTrainId()
{
    for(int i=0;i<SaloonCnt;i++)
    {
        QLabel *trainid = new QLabel(this);
        trainid->setStyleSheet("background-color:transparent;font:15pt 'Arial';color:white;");
        trainid->setGeometry(m_nSalStartx+(m_nSaloonWidth-3*m_nSaloonDorWidth)/2+i*m_nSaloonWidth,
                            m_nCabStarty+(m_nSaloonHeight-m_nSaloonDorNumHeight)/2+12,
                            3*m_nSaloonDorWidth,m_nSaloonDorNumHeight);
        trainid->setAlignment(Qt::AlignCenter);
        trainid->setText(QString("08000%1").arg(i+1));
        m_lTrainId.append(trainid);
    }
}

void Train::InitPans()
{
    for(int i=0;i<2;i++)
    {
        QLabel *pan = new QLabel(this);
        if(i==0)
            pan->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        else
            pan->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        pan->setGeometry(m_nSalStartx+(m_nSaloonWidth-m_nSaloonDorWidth)/2+m_nSaloonWidth+3*i*m_nSaloonWidth,
                            8,
                            m_nSaloonDorWidth,m_nSaloonDorHeight);
        m_lPanList.append(pan);
    }
}

void Train::InitPECUs()
{
//    for(int i=0;i<SaloonCnt;i++)
//    {
//        m_lPECUList[i].clear();
//        for(int j =0;j<4;j++)
//        {
//            QLabel *pecu = new QLabel(this);
//            pecu->setStyleSheet("border-image: url(:/Images/PECU_talkingToCrew.png);");
//            SetPosition(pecu,CAR(i),ROW(i<2?(j%2?9:1):(j%2?1:9)),COL(j),TOTAL(4),i>1);
//            m_lPECUList[i].append(pecu);
//        }
//    }
    for(int i=0;i<SaloonCnt;i++)
    {
        QPushButton *pecubtn = new QPushButton(this);
        pecubtn->setStyleSheet("border-image: url(:/Images/PECU_talkingToOcc.png);");
        SetPosition(pecubtn,CAR(i),ROW(3),COL(0),TOTAL(4),i>=SaloonCnt/2);
        pecubtn->hide();
        connect(pecubtn,SIGNAL(clicked()),this,SIGNAL(GoToPECUPage()));
        m_lPECUList.append(pecubtn);
        for(int j=0;j<4;j++)
            m_lPECUStatValue[i].append(0);
    }
}

void Train::InitIdlings()
{
    for(int i=0;i<SaloonCnt;i++)
    {
        QLabel *idling = new QLabel(this);
        idling->setStyleSheet("border-image: url(:/Images/Idling.png);");
        SetPosition(idling,CAR(i),ROW(4),COL(3),TOTAL(4),i>=SaloonCnt/2);
        idling->hide();
        m_lIdlingList.append(idling);
    }
}


void Train::InitFires()
{
    for(int i=0;i<SaloonCnt;i++)
    {
        QPushButton *firebtn = new QPushButton(this);
        firebtn->setStyleSheet("border-image: url(:/Images/FireDetect.png);");
        SetPosition(firebtn,CAR(i),ROW(3),COL(3),TOTAL(4),i>=SaloonCnt/2);
        connect(firebtn,SIGNAL(clicked()),this,SIGNAL(GoToFirePage()));
        firebtn->hide();
        m_lFireList.append(firebtn);
    }
}

void Train::InitSlippings()
{
    for(int i=0;i<SaloonCnt;i++)
    {
        QLabel *slip = new QLabel(this);
        slip->setStyleSheet("border-image: url(:/Images/SlipDetect.png);");
        SetPosition(slip,CAR(i),ROW(4),COL(0),TOTAL(4),i>=SaloonCnt/2);
        slip->hide();
        m_lSlipList.append(slip);
    }
}

void Train::InitIES()
{
    for(int i=0;i<2;i++)
    {
        QLabel *hs = new QLabel(this);
        hs->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        SetPosition(hs,CAR(i%2?4:1),ROW(LAYS-2),COL(1),TOTAL(4),i>0);
        m_lIESList.append(hs);
    }
}

void Train::setTrainID(uint8_t id)
{
    m_nTrainID = id;
    for(int i=0;i<SaloonCnt;i++)
    {
        m_lTrainId[i]->setText(QString("%1%2%3").arg(m_nLineID,2,10,QChar('0')).arg(id,2,10,QChar('0')).arg(i+1,2,10,QChar('0')));
    }
}

void Train::setLineID(uint8_t id)
{
    m_nLineID = id;
    for(int i=0;i<SaloonCnt;i++)
    {
        m_lTrainId[i]->setText(QString("%1%2%3").arg(id,2,10,QChar('0')).arg(m_nTrainID,2,10,QChar('0')).arg(i+1,2,10,QChar('0')));
    }
}

void Train::setPECUStatus(uint8_t car, uint8_t index, uint8_t stat)
{
    if(index > 3)
        return;
    m_lPECUStatValue[car][index] = stat;
    if(m_lPECUStatValue[car].contains(2) || m_lPECUStatValue[car].contains(3))
        m_lPECUList[car]->show();
    else
        m_lPECUList[car]->hide();
}

void Train::setCabStat(int cab, uint8_t stat)
{
    switch (stat) {
    case 0:
        m_lCabinList[cab]->setStyleSheet("background-color:rgb(192,192,192);border:1px solid rgb(117,117,117);color:black;font:12pt 'Arial';");
        break;
    case 1:
        m_lCabinList[cab]->setStyleSheet("background-color:rgb(0,100,255);border:1px solid rgb(117,117,117);color:white;font:12pt 'Arial';");
        break;
    case 2:
        m_lCabinList[cab]->setStyleSheet("border:1px solid rgb(117,117,117);color:white;font:12pt 'Arial';");
        break;
    default:
        m_lCabinList[cab]->setStyleSheet("background-color:rgb(192,192,192);border:1px solid rgb(117,117,117);color:white;font:12pt 'Arial';");
        break;
    }
}

void Train::setDirection(uint8_t dir)
{
    m_lDirectionList[0]->setStyleSheet("border-image: url(:/Images/Forward.png);");
    m_lDirectionList[1]->setStyleSheet("border-image: url(:/Images/Backward.png);");

    if(dir == 0)
    {
        m_lDirectionList[0]->setStyleSheet("border-image: url(:/Images/ForwardUnknown.png);");
        m_lDirectionList[1]->setStyleSheet("border-image: url(:/Images/BackwardUnKnown.png);");
    }
    m_lDirectionList[0]->setVisible(dir==0 || dir==2);
    m_lDirectionList[1]->setVisible(dir==0 || dir==3);
}

void Train::setHSCBStat(uint8_t index, uint8_t stat)
{
    if(index >= m_lHSCBList.length())
        return;
    switch (stat) {
    case 0:
        m_lHSCBList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    case 4:
        m_lHSCBList[index]->setStyleSheet("border-image: url(:/Images/HSCB_close.png);");
        break;
    case 3:
        m_lHSCBList[index]->setStyleSheet("border-image: url(:/Images/HSCB_open.png);");
        break;
    case 1:
        m_lHSCBList[index]->setStyleSheet("border-image: url(:/Images/HSCB_openflt.png);");
        break;
    case 2:
        m_lHSCBList[index]->setStyleSheet("border-image: url(:/Images/HSCB_closeflt.png);");
        break;
    default:
        m_lHSCBList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    }
}

void Train::setTracConvertStat(uint8_t index, uint8_t stat)
{
    if(index >= m_lTracConvertList.length())
        return;
    switch (stat) {
    case 0:
        m_lTracConvertList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    case 1:
        m_lTracConvertList[index]->setStyleSheet("border-image: url(:/Images/TracFlt.png);");
        break;
    case 2:
        m_lTracConvertList[index]->setStyleSheet("border-image: url(:/Images/TracWork.png);");
        break;
    case 3:
        m_lTracConvertList[index]->setStyleSheet("border-image: url(:/Images/TracStop.png);");
        break;
    case 4:
        m_lTracConvertList[index]->setStyleSheet("border-image: url(:/Images/TracNoSeriousFlt.png);");
        break;
    default:
        m_lTracConvertList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    }
}

void Train::setTSKStat(uint8_t index, uint8_t stat)
{
    if(index >= m_lTSKList.length())
        return;
    switch (stat) {
    case 0:
        m_lTSKList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    case 1:
        m_lTSKList[index]->setStyleSheet("border-image: url(:/Images/TSK_close.png);");
        break;
    case 2:
        m_lTSKList[index]->setStyleSheet("border-image: url(:/Images/TSK_open.png);");
        break;
    default:
        m_lTSKList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    }
}

void Train::setIdlingStat(uint8_t index, uint8_t stat)
{
    if(index >= m_lIdlingList.length())
        return;
    m_lIdlingList[index]->raise();
    if(stat)
        m_lIdlingList[index]->show();
    else
        m_lIdlingList[index]->hide();
}

void Train::setSlipingStat(uint8_t index, uint8_t stat)
{
    if(index >= m_lSlipList.length())
        return;
    m_lSlipList[index]->raise();
    if(stat)
        m_lSlipList[index]->show();
    else
        m_lSlipList[index]->hide();
}

void Train::setAuxConvertStat(uint8_t index, uint8_t stat)
{
    if(index >= m_lAuxConvertList.length())
        return;
    switch (stat) {
    case 0:
        m_lAuxConvertList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    case 1:
        m_lAuxConvertList[index]->setStyleSheet("border-image: url(:/Images/AuxFlt.png);");
        break;
    case 2:
        m_lAuxConvertList[index]->setStyleSheet("border-image: url(:/Images/AuxWork.png);");
        break;
    case 3:
        m_lAuxConvertList[index]->setStyleSheet("border-image: url(:/Images/AuxStop.png);");
        break;
    case 4:
        m_lAuxConvertList[index]->setStyleSheet("border-image: url(:/Images/AuxNotSeriousFlt.png);");
        break;
    default:
        m_lAuxConvertList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    }
}

void Train::setBatChargerStat(uint8_t index, uint8_t stat)
{
    if(index >= m_lBatChargerList.length())
        return;
    switch (stat) {
    case 0:
        m_lBatChargerList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    case 1:
        m_lBatChargerList[index]->setStyleSheet("border-image: url(:/Images/ChargerFlt.png);");
        break;
    case 2:
        m_lBatChargerList[index]->setStyleSheet("border-image: url(:/Images/ChargerWork.png);");
        break;
    case 3:
        m_lBatChargerList[index]->setStyleSheet("border-image: url(:/Images/ChargerStop.png);");
        break;
    default:
        m_lBatChargerList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    }
}

void Train::setServiceBrkStat(uint8_t car, uint8_t index, uint8_t stat)
{
    if(index >= m_lServiceBrake[car].length())
        return;
    switch (stat) {
    case 0:
        m_lServiceBrake[car][index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    case 2:
        m_lServiceBrake[car][index]->setStyleSheet("border-image: url(:/Images/ServiceBrkFlt.png);");
        break;
    case 3:
        m_lServiceBrake[car][index]->setStyleSheet("border-image: url(:/Images/ServiceBrkApp.png);");
        break;
    case 4:
        m_lServiceBrake[car][index]->setStyleSheet("border-image: url(:/Images/ServiceBrkRel.png);");
        break;
    case 1:
        m_lServiceBrake[car][index]->setStyleSheet("border-image: url(:/Images/Iso.png);");
        break;
    default:
        m_lServiceBrake[car][index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    }
}

void Train::setParkingBrakeStat(uint8_t index, uint8_t stat)
{
    if(index >= m_lParkingBrakeList.length())
        return;
    switch (stat) {
    case 0:
        m_lParkingBrakeList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    case 2:
        m_lParkingBrakeList[index]->setStyleSheet("border-image: url(:/Images/ParkBrkFlt.png);");
        break;
    case 4:
        m_lParkingBrakeList[index]->setStyleSheet("border-image: url(:/Images/ParkBrkApp.png);");
        break;
    case 3:
        m_lParkingBrakeList[index]->setStyleSheet("border-image: url(:/Images/ParkBrkRel.png);");
        break;
    case 1:
        m_lParkingBrakeList[index]->setStyleSheet("border-image: url(:/Images/Iso.png);");
        break;
    default:
        m_lParkingBrakeList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    }
}

void Train::setAirCompressorStat(uint8_t index, uint8_t stat)
{
    if(index >= m_lAirCompressList.length())
        return;
    switch (stat) {
    case 0:
        m_lAirCompressList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    case 1:
        m_lAirCompressList[index]->setStyleSheet("border-image: url(:/Images/AirCompFlt.png);");
        break;
    case 2:
        m_lAirCompressList[index]->setStyleSheet("border-image: url(:/Images/AirCompWork.png);");
        break;
    case 3:
        m_lAirCompressList[index]->setStyleSheet("border-image: url(:/Images/AirCompStop.png);");
        break;
    default:
        m_lAirCompressList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    }
}

void Train::setEmDorStat(uint8_t index,uint8_t stat)
{
    if(index >= m_lEmrDorList.length())
        return;
    switch (stat) {
    case 0:
        m_lEmrDorList[index]->setStyleSheet("background-color:rgb(157,157,157);border:1px solid rgb(157,157,157);");
        break;
    case 1:
        m_lEmrDorList[index]->setStyleSheet("background-color:rgb(0,255,0);border:1px solid rgb(0,255,0);");
        break;
    case 2:
        m_lEmrDorList[index]->setStyleSheet("background-color:rgb(255,255,0);border:1px solid rgb(255,255,0);");
        break;
    case 3:
        m_lEmrDorList[index]->setStyleSheet("background-color:rgb(255,0,0);border:1px solid rgb(255,0,0);");
        break;
    default:
        m_lEmrDorList[index]->setStyleSheet("background-color:rgb(157,157,157);border:1px solid rgb(157,157,157);");
        break;
    }
}

void Train::setSaloonDorStat(uint8_t car, uint8_t index, uint8_t stat)
{
    if(car >= SaloonCnt)
        return;
    if(index >= m_lSaloonDorList[car].length())
        return;
    switch (stat) {
    case 0:
        m_lSaloonDorList[car][index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    case 1:
        m_lSaloonDorList[car][index]->setStyleSheet("border-image: url(:/Images/Iso.png);");
        break;
    case 2:
        m_lSaloonDorList[car][index]->setStyleSheet("border-image: url(:/Images/DorEmOpen.png);");
        break;
    case 3:
        m_lSaloonDorList[car][index]->setStyleSheet("border-image: url(:/Images/DorObstacle.png);");
        break;
    case 4:
        m_lSaloonDorList[car][index]->setStyleSheet("border-image: url(:/Images/DorOpenFlt.png);");
        break;
    case 5:
        m_lSaloonDorList[car][index]->setStyleSheet("border-image: url(:/Images/DorCloseFlt.png);");
        break;
    case 6:
        m_lSaloonDorList[car][index]->setStyleSheet("border-image: url(:/Images/DorOpen.png);");
        break;
    case 7:
        m_lSaloonDorList[car][index]->setStyleSheet("border-image: url(:/Images/DorEmReq.png);");
        break;
    case 8:
        m_lSaloonDorList[car][index]->setStyleSheet("border-image: url(:/Images/DorClose.png);");
        break;
    default:
        m_lSaloonDorList[car][index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    }
}

void Train::setFireStat(uint8_t index, uint8_t stat)
{
    if(index >= m_lFireList.length())
        return;
    m_lFireList[index]->raise();
    if(stat)
        m_lFireList[index]->show();
    else
        m_lFireList[index]->hide();
}

void Train::setPanStat(uint8_t index, uint8_t stat)
{
    if(index >= m_lPanList.length())
        return;
    switch (stat) {
    case 0:
        m_lPanList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    case 3:
        m_lPanList[index]->setStyleSheet(index==0?"border-image: url(:/Images/PanRaiseL.png);":"border-image: url(:/Images/PanRaiseR.png);");
        break;
    case 4:
        m_lPanList[index]->setStyleSheet(index==0?"border-image: url(:/Images/PanLowL.png);":"border-image: url(:/Images/PanLowerR.png);");
        break;
    case 1:
        m_lPanList[index]->setStyleSheet(index==0?"border-image: url(:/Images/PanLowLADDFlt.png);":"border-image: url(:/Images/PanLowerRADDFlt.png);");
        break;
    case 2:
        m_lPanList[index]->setStyleSheet(index==0?"border-image: url(:/Images/PanLowLFlt.png);":"border-image: url(:/Images/PanLowerRFlt.png);");
        break;
    default:
        m_lPanList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    }
}

void Train::setIESStat(uint8_t index, uint8_t stat)
{
    if(index >= m_lIESList.length())
        return;
    switch (stat) {
    case 0:
        m_lIESList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    case 2:
        m_lIESList[index]->setStyleSheet("border-image: url(:/Images/HVSSwitch_PAN.png);");
        break;
    case 1:
        m_lIESList[index]->setStyleSheet("border-image: url(:/Images/HVSSwitch_WOS.png);");
        break;
    case 3:
        m_lIESList[index]->setStyleSheet("border-image: url(:/Images/HVSSwitch_Key.png);");
        break;
    default:
        m_lIESList[index]->setStyleSheet("border-image: url(:/Images/Unknow.png);");
        break;
    }
}

void Train::setEffDmd(bool tracTL,bool brkTL,bool FBTL,uint16_t value)
{
    if(value > 10000)
        return;
    tbfbar_->setEffDmd(tracTL,brkTL,FBTL,value/100);
}

void Train::setACModeStat(uint8_t index,bool stat)
{
    if(index >= m_lACModeList.length())
        return;
    if(stat)
        m_lACModeList[index]->setStyleSheet("border-image: url(:/Images/WOS_on.png);");
    else
        m_lACModeList[index]->setStyleSheet("border-image: url(:/Images/WOS_off.png);");
}
